package com.study;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.study.config.SpringConfiguration;
import com.study.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.util.ResourceBundle;

public class demo4 {

	@Test
	//测试Spring容器产生数据源对象
	//xml---
	public void test4() throws Exception{
		ApplicationContext applicationContext =
				new ClassPathXmlApplicationContext("applicationContext.xml");
		DataSource dataSource = applicationContext.getBean(DataSource.class);
		Connection connection = dataSource.getConnection();
		System.out.println(connection);
		connection.close();

	}

	@Test
	//测试手动创建c3p0数据源（加载properties配置文件）
	public void test3() throws Exception {
		//读取配置文件
		ResourceBundle rb = ResourceBundle.getBundle("jdbc");
		String driver = rb.getString("jdbc.driver");
		String url = rb.getString("jdbc.url");
		String username = rb.getString("jdbc.username");
		String password = rb.getString("jdbc.password");
		//创建数据源对象 设置连接参数
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setDriverClass(driver);
		dataSource.setJdbcUrl(url);
		dataSource.setUser(username);
		dataSource.setPassword(password);

		Connection connection = dataSource.getConnection();
		System.out.println(connection);
		connection.close();

	}

}
